package com.gmail.hh.flickrj.sample.android.listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageView;

import com.gmail.hh.flickrj.sample.android.FlickrjMainActivity;
import com.gmail.hh.flickrj.sample.android.data.PhotoData;
import com.gmail.hh.flickrj.sample.android.data.PhotoData.IPhotoPositionShift;

/**
 * 
 * @author Helmut
 *
 */
public class ImageViewZoomListener implements
											OnClickListener, 
											OnLongClickListener,
											OnTouchListener { 
	
	private static final Logger logger = LoggerFactory.getLogger(FlickrjMainActivity.class);
	
	private PhotoData photoData;
	IPhotoPositionShift callbackPhotoPositionShift;

	
	// Constructor
	public ImageViewZoomListener() {
		super();
	}

	//  Always needs a photoData Object, when an action is applied!
	public void setPhotoData(PhotoData photoData) {
		this.photoData = photoData;
	}
	public void setIPhotoPositionShift(IPhotoPositionShift callbackPhotoPositionShift) {
		this.callbackPhotoPositionShift = callbackPhotoPositionShift;
	}
	
	
	@Override
	public void onClick(View v) {
        if (photoData != null ) {
        	photoData.centerMatrix(PhotoData.IMAGE_CENTERROT_CENTER, photoData.getImageViewMedium() );
    	}
	    logger.error("ImageViewZoomListener - click executed");
	}


	@Override
	public boolean onLongClick(View v) {
        if (photoData != null ) {
        	photoData.centerMatrix(PhotoData.IMAGE_CENTERROT_CENTERROT, photoData.getImageViewMedium() );
    	}
	    logger.error("ImageViewZoomListener - longClick executed");
		return true;  	// stop propagation true/false
						// when return false, then its possible that move/zoom is failing
	}
	


	@Override
	public boolean onTouch(View v, MotionEvent event) {
	 //   dumpEvent(event);
        if (photoData != null ) {
        	photoData.applyTouchEvent(v, event, callbackPhotoPositionShift);
    	}
		return true;  	// stop propagation true/false
	    				// when return false, then its possible that move/zoom is failing
	}
	
	
	private void dumpEvent(MotionEvent event) {
	    String names[] = { "DOWN", "UP", "MOVE", "CANCEL", "OUTSIDE",
	            "POINTER_DOWN", "POINTER_UP", "7?", "8?", "9?" };
	    StringBuilder sb = new StringBuilder();
	    int action = event.getAction();
	    int actionCode = action & MotionEvent.ACTION_MASK;
	    sb.append("event ACTION_").append(names[actionCode]);
	    if (actionCode == MotionEvent.ACTION_POINTER_DOWN
	            || actionCode == MotionEvent.ACTION_POINTER_UP) {
	        sb.append("(pid ").append(
	                action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
	        sb.append(")");
	    }
	    sb.append("[");
	    for (int i = 0; i < event.getPointerCount(); i++) {
	        sb.append("#").append(i);
	        sb.append("(pid ").append(event.getPointerId(i));
	        sb.append(")=").append((int) event.getX(i));
	        sb.append(",").append((int) event.getY(i));
	        if (i + 1 < event.getPointerCount())
	            sb.append(";");
	    }
	    sb.append("]");
	    logger.error(sb.toString());
	}


}
